package cool.houge.pig.infra.web;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.http.codec.json.Jackson2JsonDecoder;
import org.springframework.http.codec.json.Jackson2JsonEncoder;
import org.springframework.web.reactive.config.WebFluxConfigurer;

/**
 * WebFlux 配置.
 *
 * @author KK (kzou227@qq.com)
 */
@Configuration
public class CustomWebFluxConfigurer implements WebFluxConfigurer {

  private final Jackson2JsonEncoder jackson2JsonEncoder;
  private final Jackson2JsonDecoder jackson2JsonDecoder;

  public CustomWebFluxConfigurer(
      Jackson2JsonEncoder jackson2JsonEncoder, Jackson2JsonDecoder jackson2JsonDecoder) {
    this.jackson2JsonEncoder = jackson2JsonEncoder;
    this.jackson2JsonDecoder = jackson2JsonDecoder;
  }

  @Override
  public void configureHttpMessageCodecs(ServerCodecConfigurer configurer) {
    configurer.defaultCodecs().jackson2JsonEncoder(jackson2JsonEncoder);
    configurer.defaultCodecs().jackson2JsonDecoder(jackson2JsonDecoder);
  }
}
